/**********************************************************************************************/
/* FBL_EscapeString.cpp 					                                  				  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2006															  */
/* All Rights Reserved.                                                 					  */
/**********************************************************************************************/

#include <VShared/FBL/publ/Headers/StdAfx.h>

#include <VShared/FBL/publ/Util_Strings/FBL_EscapeString.h>
#include <VShared/FBL/publ/Util_Strings/FBL_ustdio.h>


/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
UChar* EscapeStringU( const UChar* inStr, bool inRegEx )
{
	argused1( inRegEx );
	
	if( inStr == NULL )
		return NULL;

	//in the worse case, each character may need to be encoded as 
	//using two chars, and we need room for the terminating null byte too
	UChar* pNew = new UChar[2 * vu_strlen(inStr) + 1];
	const UChar* p = inStr;
	UChar* q = pNew;

	while( 1 )
	{
		switch( *p )
		{
			case UChar('\''):
			case UChar('\\'):
			{
				*q++ = UChar('\\');
			}break;
			case UChar('\0'):
			{
				*q = UChar('\0');
				goto exit;
			}break;
		}

		*q++ = *p++;
	}

exit:
	return pNew;
}


/**********************************************************************************************/
char* EscapeStringA( const char* inStr, bool inRegEx )
{
	argused1( inRegEx );
	
	if( inStr == NULL )
		return NULL;

	//in the worse case, each character may need to be encoded as 
	//using two chars, and we need room for the terminating null byte too
	char* pNew = new char[2 * strlen(inStr) + 1];
	const char* p = inStr;
	char* q = pNew;

	while( 1 )
	{
		switch( *p )
		{
			case '\'':
			case '\\':
			{
				*q++ = '\\';
			}break;
			case '\0':
			{
				*q = '\0';
				goto exit;
			}break;
		}

		*q++ = *p++;
	}

exit:
	return pNew;
}


/**********************************************************************************************/
void FreeStringU( UChar* inStr )
{
	delete []inStr;
}


/**********************************************************************************************/
void FreeStringA( char* inStr )
{
	delete []inStr;
}


/**********************************************************************************************/
FBL_End_Namespace
